package com.example.aop.proxy;

import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

/**
 * ClassName: Main
 * Package: IntelliJ IDEA
 * Description:
 *
 * @Author 谷京京
 * @Create 2024/1/15 16:06
 * @Version 1.0
 */
public class Main {
    public static void main(String[] args) {
        HouseSubject subject = new RealHouseSubject();

        HouseSubjectClass subjectClass = new HouseSubjectClass();
        //静态代理
//        HouseProxy proxy = new HouseProxy(subject);
//        proxy.rentHouse();
        //动态代理
        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(subject.getClass().getClassLoader(),
                new Class[]{HouseSubject.class},
                new JDKInvocation(subject));
        proxy.saleHouse();
        proxy.rentHouse();
        //错误
//        HouseSubjectClass proxy2 = (HouseSubjectClass) Proxy.newProxyInstance(subjectClass.getClass().getClassLoader(),
//                new Class[]{HouseSubjectClass.class},
//                new JDKInvocation(subjectClass));
//        proxy2.rentHouse();

        //CGLib动态代理
        HouseSubject proxy2 = (HouseSubject) Enhancer.create(subject.getClass(), new CGLibIntercepter(subject));
        proxy2.rentHouse();
        //代理类
        HouseSubjectClass proxy3 = (HouseSubjectClass) Enhancer.create(subjectClass.getClass(), new CGLibIntercepter(subjectClass));
        proxy3.rentHouse();
    }
}
